package com.xzy.controller.g;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * Set-Cookie
 *
 * @author xzy.xiao
 * @date 2022/9/19  11:02
 */
@RestController
@RequestMapping(path = "/test/set_cookie")
public class TestController {

    Logger logger = LoggerFactory.getLogger(TestController.class);

    @GetMapping("/test")
    public String test(HttpServletRequest request, HttpServletResponse response) {
        String oldToken = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if ("token".equals(cookie.getName())) {
                oldToken = cookie.getValue();
            }
        }

        String newToken = "token=" + UUID.randomUUID();
        response.setHeader("Set-Cookie", newToken);

        logger.info("请求携带的Token：{}    响应返回的Token：{}", oldToken, newToken);
        return "Set-Cookie Test";
    }
}
